package cn.dubito.manager.service;

import cn.dubito.api.ProductRpc;
import cn.dubito.api.domain.ParamInf;
import cn.dubito.api.domain.ProductRpcParam;
import cn.dubito.entity.Product;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liliang
 * @description: RPC 服务实现类
 * @date 2018/5/27.
 */
@AutoJsonRpcServiceImpl
@Service
public class ProductRpcImpl implements ProductRpc {

    private static Logger LOG = LoggerFactory.getLogger(ProductRpcImpl.class);

    @Autowired
    private ProductService productService;

    @Override
    public List<Product> query(ParamInf param) {
        LOG.info("查询多个产品,请求参数:{}", param);
        Pageable pageable = new PageRequest(0, 100, Sort.Direction.DESC, "rewardRate");
        Page<Product> result = productService.query(param.getIdList(), param.getMinRewardRate(),
                param.getMaxRewardRate(), param.getStatusList(), pageable);

        LOG.info("查询多个产品返回结果:{}", result);

        return result.getContent();
    }


    @Override
    public Product findById(String id) {
        LOG.info("查询单个产品,请求参数:{}", id);

        Product result = productService.findProduct(id);

        LOG.info("查询单个产品返回结果:{}", result);

        return result;
    }
}
